/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils","base"],function(utils,baseClass){

  var VBoxRow = function(config){
      var _this = this;
      var style = config.style ||{};
      var overflowStyle={
        overflow:null,
        overflowX:null,
        overflowY:null,
        padding: null // TODO 询问方法
      };

      for(var key in overflowStyle){
        if(style[key]){
          overflowStyle[key] = style[key];
          delete style[key];
        }else{
          delete overflowStyle[key];
        }
      }


      VBoxRow.baseConstructor.call(this,config);
      this.$el.addClass("yy-vbox-row");



      this.CellDom = $("<div class='yy-vbox-cell'></div>");
      this.CellDom.css(overflowStyle);
      if(style.height){
        this.CellDomInner = $("<div style='height:"+style.height+"' class='yy-vbox-cell-inner'></div>");
        this.$$childrenWrapper = this.CellDomInner;
        this.CellDom.append(this.CellDomInner);
      }else{
        this.$$childrenWrapper = this.CellDom;
      }


      this.initLayout(this.datasource,null,this.rowInstance);

      this.$el.append(this.CellDom);
  }

  utils.extendClass(VBoxRow,baseClass);

    var Component = function(config){
        var _this = this;
        Component.baseConstructor.call(this,config);
        this.$el.addClass("yy-vbox");
        this.components=[];
        this.layout();
    };

    utils.extendClass(Component,baseClass);

    Component.prototype.layout = function(){
      var items = this.config.items||[];
      if(items.length>3){
        alert("vbox为了某些IE兼容性的原因还是不要超过3个子元素");
      }
      items = this.getLayout(items);
      for(var i=0,j= items.length;i<j;i++){
        var itemData = items[i];
        var vboxRow =  new VBoxRow({
          type:"vboxrow",
          root:itemData.root,
          style:itemData.style,
          $$datasource:this.datasource,
          $$parent:this,
          $$pageview:this.pageview,
          $$rowInstance:this.$$rowInstance
        });
        this.components.push(vboxRow);
        this.$el.append(vboxRow.$el);
      }

    };

    Component.prototype.getLayout = function(items){
      if(items.length===1){
        items[0].style.top = 0;
        items[0].style.bottom = 0;
        items[0].style.left = 0;
        items[0].style.right = 0;
        delete  items[0].style.height;
      }
      if(items.length==2){
        items[0].style= items[0].style||{};
        var item1Height = items[0].style.height;
        var item2Height = items[1].style.height;
        if(!item1Height&&!item2Height){
          console.error("vbox 必须有个设置style高度");
        }
        if(item1Height){
          items[0].style.top = 0;
          items[0].style.height =item1Height;
          items[0].style.left = 0;
          items[0].style.right = 0;

          items[1].style.top = item1Height;
          items[1].style.bottom = 0;
          items[1].style.left = 0;
          items[1].style.right = 0;
        }else{
          items[0].style.top = 0;
          items[0].style.bottom =item2Height;
          items[0].style.left = 0;
          items[0].style.right = 0;

          items[1].style.height = item2Height;
          items[1].style.bottom = 0;
          items[1].style.left = 0;
          items[1].style.right = 0;
        }
      }

      if(items.length==3){
        var item1Height = items[0].style.height;
        var item2Height = items[1].style.height;
        if(item1Height&&item2Height){
            items[0].style.top = 0;
            items[0].style.height =item1Height;
            items[0].style.left = 0;
            items[0].style.right = 0;

          items[1].style.top = item1Height;
          items[1].style.height = item2Height;
          items[1].style.left = 0;
          items[1].style.right = 0;

          items[2].style.top = parseInt(item1Height)+parseInt(item2Height);
          items[2].style.bottom = 0;
          items[2].style.left = 0;
          items[2].style.right = 0;
          return items;
        }
        var item3Height = items[2].style.height;
        if(!item1Height&&!item3Height){
          console.error("vbox 必须有个设置第一个和最后一个style高度");
        }

        items[0].style.top = 0;
        items[0].style.height =item1Height;
        items[0].style.left = 0;
        items[0].style.right = 0;

        items[1].style.top = item1Height;
        items[1].style.bottom = item3Height;
        items[1].style.left = 0;
        items[1].style.right = 0;
        delete items[1].style.height;

        items[2].style.height = item3Height;
        items[2].style.bottom = 0;
        items[2].style.left = 0;
        items[2].style.right = 0;

      }

      return items;
    }

    return Component;
});
